package com.wswy.wzcx.ui.coupon

import android.graphics.*
import android.graphics.drawable.Drawable
import android.os.Build


class InsideCornersDrawable(private val bgDrawable :Drawable?) : Drawable() {


    var topLeftRadius: Float = 0f

    var topRightRadius: Float = 0f

    var bottomRightRadius: Float = 0f

    var bottomLeftRadius: Float = 0f

    private val mRect = Rect()
    private val mRectF = RectF()

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val mXorMode = PorterDuffXfermode(PorterDuff.Mode.XOR)


    override fun getOpacity(): Int {
        return PixelFormat.TRANSPARENT
    }

    override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
        super.setBounds(left, top, right, bottom)
        mRect.set(left, top, right, bottom)
    }

    override fun draw(mycanvas: Canvas?) {
        mycanvas?.let { canvas ->

            mRectF.set(mRect)
            val saveCount =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.saveLayer(mRectF,null)
            }else{
                canvas.saveLayer(mRectF, null, Canvas.ALL_SAVE_FLAG)
            }



            canvas.drawARGB(0,0,0,0)


            mPaint.xfermode = null

            bgDrawable?.bounds = mRect
            bgDrawable?.draw(canvas)

            mPaint.xfermode = mXorMode
            mPaint.color = Color.RED

            if(topLeftRadius > 0){
                canvas.drawCircle(0f, 0f, topLeftRadius, mPaint)
            }

            if(topRightRadius > 0){
                canvas.drawCircle(mRectF.right, 0f, topRightRadius, mPaint)
            }

            if(bottomLeftRadius > 0){
                canvas.drawCircle(0f, mRectF.bottom, bottomLeftRadius, mPaint)
            }

            if(bottomRightRadius > 0){
                canvas.drawCircle(mRectF.right, mRectF.bottom, bottomRightRadius, mPaint)
            }

            canvas.restoreToCount(saveCount)


        }


    }

    override fun setAlpha(alpha: Int) {
        mPaint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mPaint.colorFilter = colorFilter

    }


}